<template>
  <div id="tableConfig">

    <div class="columnsInfo">
      <h4 class="titleStr" style="line-height: 40px; background-color: rgb(239, 243, 247); display: flex"> <div class="verticalBar"/>{{$t('outbound.skuTypeGroup')}}</h4>
      <el-form :model="skuTypeGroupObject" :rules="rules" ref="ruleForm" :inline="true" label-width="100px" class="demo-ruleForm" >
        <el-form-item :label="$t('outbound.skuTypeGroupCode')" prop="groupNo" class="base-width-50">
          <el-input type="text" autosize v-model="skuTypeGroupObject.groupNo"></el-input>
        </el-form-item>
        <el-form-item :label="$t('outbound.skuTypeGroupName')" prop="groupName" class="base-width-50">
          <el-input type="text" autosize v-model="skuTypeGroupObject.groupName"></el-input>
        </el-form-item>
        <el-form-item :label="$t('app.remark')" prop="remark" class="base-width-100">
          <el-input type="textarea" autosize v-model="skuTypeGroupObject.remark"></el-input>
        </el-form-item>

        <h4 class="titleStr" style="margin-top: 25px; line-height: 40px; background-color: rgb(239, 243, 247); display: flex"> <div class="verticalBar"/>{{$t('outbound.skuClass')}}</h4>

        <!-- 树状图 -->
        <el-tree :style="`height:${dynamicHeight}` + 'px'" :data="treeData" show-checkbox node-key="id" :default-expanded-keys="checkedKeys"
                 :default-checked-keys="checkedKeys" :props="defaultProps" @check="setCheckedFunc" ref="tree">
        </el-tree>

        <!-- 保存|取消 -->
        <div class="btn_b">
          <el-button type="primary" :disabled="!this.btnPermNameList.includes('EN021004')" @click="saveObj('ruleForm')">{{$t("common.save")}}</el-button>
          <el-button @click="cancel" :disabled="!this.btnPermNameList.includes('EN021005')" >{{$t("common.cancel")}}</el-button>
        </div>
      </el-form>
    </div>

    <div class="columnsSetting">


    </div>
  </div>
</template>

<script>

import {
  insertSkuTypeGroup,
  updateSkuTypeGroup,
  querySkuTypeGroupById,
  querySkuTypeTreeData
} from "@/api/skuTypeGroup/skuTypeGroup";
import baseMixin from "@/mixins/BaseMixin";

export default {
  mixins: [baseMixin],
  name: "skuTypeGroupDrawer",
  data() {
    return {
      // 主表
      skuTypeGroupObject: {
        groupNo: undefined,
        groupName: undefined,
        remark: undefined,
        skuTypeGroupDtls: undefined
      },
      // 子表
      dynamicHeight: 0,
      all: [],
      treeData: [],
      defaultProps: {
        children: 'node',
        label: 'typeName'
      },
      checkedKeys: [],
      newFlag: false,
      copyFlag: false,
      rules: {
        groupNo: [{required: true, message: this.$inputPlease("config.groupNo"), trigger: 'change'}],
        groupName: [{required: true, message: this.$inputPlease("config.groupName"), trigger: 'change'}]
      },
      keyFlag: true,
      excelImportTypes: []
    }
  },
  methods: {

    querySkuTypeTreeData(data) {
      querySkuTypeTreeData(data).then(res =>{
        this.all = res.data.all;
        this.treeData = res.data.treeList;
        this.checkedKeys = res.data.checkedList;
      })
    },
    setCheckedFunc(data, nodes) {
      console.log('setCheckedFunc')
      this.reCheckedKeys = nodes.checkedKeys;
    },
    getAllCheckedKeys() {
      // 目前被选中的部门节点
      let checkedKeys = this.$refs.tree.getCheckedKeys();
      // 半选中的部门节点
      // let halfCheckedKeys = this.$refs.tree.getHalfCheckedKeys();
      // checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    save(row, index) {
      index += 1;
      // 验证参数是否存在
      if (row.fieldCode === undefined) {
        return this.$message .warning(this.$t('msg.msg40',{no:index}));
      }
      if (row.fieldName === undefined) {
        return this.$message .warning(this.$t('msg.msg41',{no:index}));
      }
      if (row.fieldWidth === undefined) {
        return this.$message .warning(this.$t('msg.msg42',{no:index}));
      }
      row.isEditor = !row.isEditor;
      row.isSave = !row.isSave;
      this.keyFlag = !this.keyFlag;
    },
    edit(row, index) {
      console.log("edit下标 = %o， 对象信息 = %o", index, row.isEditor);
      row.isEditor = !row.isEditor;
      row.isSave = !row.isSave;
      this.keyFlag = !this.keyFlag;
    },
    toSaveRequest() {
      insertSkuTypeGroup(this.skuTypeGroupObject).then(res => {
          this.$message.success(this.$t('app.saveMes'));
          this.cancel();
      });
    },
    toEditRequest() {
      console.log(this.skuTypeGroupObject);
      updateSkuTypeGroup(this.skuTypeGroupObject).then(res => {
          this.$message.success(this.$t('app.upMes'));
          this.cancel();
      });
    },
    logic() {
      const skuTypeGroupDtls = [];
      // 处理货品分类
      let allCheckedKeys = this.getAllCheckedKeys();
      for (const allCheckedKey of allCheckedKeys) {
        for (const tree of this.all) {
          console.log(allCheckedKey, tree.id)
          if (allCheckedKey === tree.id) {
            skuTypeGroupDtls.push({
              "typeId": tree.id,
              "typeName": tree.typeName
            })
          }
        }
      }
      this.skuTypeGroupObject.skuTypeGroupDtls = skuTypeGroupDtls;

      if (this.newFlag) {
        // 新增逻辑
        this.toSaveRequest();
      } else {
        // 编辑逻辑
        this.toEditRequest();
      }
    },
    saveObj(ruleForm) {
      // 验证参数
      this.$refs[ruleForm].validate((valid) => {
        // 验证成功
        if (valid) {
          return this.logic();
        }
        // 验证失败
        else {
          return this.$message .warning(this.$t('app.dataIsNotNull'));
        }
      });
    },
    cancel() {
      this.$router .push({name: "skuTypeGroup"})
    },
    getData() {

      // 获取编辑的数据
      querySkuTypeGroupById(this.selectData.id).then(res => {
          this.skuTypeGroupObject = res.data;
          if (this.copyFlag) {
            this.skuTypeGroupObject.id = undefined
          }
        
      });
    },
    verifyCode(e){
      for (let i = 0; i < this.tableData.length - 1; i++) {
        let item = this.tableData[i];
        if (item.fieldCode === e.fieldCode) {
          let text = (i + 1) + this.$t('msg.msg43');
          e.fieldCode = undefined;
          return this.$message .error(text);
        }
      }
    }
  },
  mounted() {

    // 动态高度
    this.dynamicHeight = this.getHeight(window, 240);

    this.newFlag = this.$store.state.newFlag;
    let query = this.$route.query;
    let data = {};

    if (!this.newFlag) {
      this.selectData = this.$store.state.routeData;
      data = {"id": this.selectData.id};
      this.getData();
    }

    this.copyFlag = query.copy;
    if (this.copyFlag) {
      this.selectData = query.obj;
      data = {"id": this.selectData.id};
      this.getData();
    }

    // 获取货品分类
    this.querySkuTypeTreeData(data);
  }
}

</script>

<style lang="scss" scoped>

.demo-ruleForm {
  .el-select {
    height: 25px;

    ::v-deep .el-input {
      height: 25px;
    }
  }
}

.btn_b {
  width: 100%;
  text-align: center;
  margin-top: 16px;
}

.verticalBar {
  width: 6px;
  margin: 10px;
  height: 2rem;
  background: rgb(51, 123, 174);
}

.perDetail {
  width: calc(100% - 250px);
  float: right;
  padding-left: 20px;
  max-height: 800px;
  overflow: hidden;

}
.el-tree {
  overflow: auto;
}
</style>
